<template>
  <div id="app">
    <nav class="navbar">
      <div class="nav-container">
        <h1 class="logo">Vue3 + TypeScript + Vite</h1>
        <ul class="nav-links">
          <li>
            <router-link to="/">Home</router-link>
          </li>
          <li>
            <router-link to="/about">About</router-link>
          </li>
          <li>
            <router-link to="/api-demo">API Demo</router-link>
          </li>
        </ul>
        <div class="user-actions">
          <template v-if="isLoggedIn">
            <span class="user-info">👤 {{ userInfo?.username || '用户' }}</span>
            <button @click="handleLogout" class="logout-btn">登出</button>
          </template>
          <template v-else>
            <router-link to="/login" class="login-link">登录</router-link>
          </template>
        </div>
      </div>
    </nav>
    <main class="main-content">
      <router-view />
    </main>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { isAuthenticated, getUserInfo, clearAuth, type UserInfo } from './utils/auth'

const router = useRouter()
const userInfo = ref<UserInfo | null>(null)
const isLoggedIn = computed(() => isAuthenticated())

// 更新用户信息
const updateUserInfo = () => {
  userInfo.value = getUserInfo()
}

// 处理登出
const handleLogout = () => {
  clearAuth()
  userInfo.value = null
  console.log('✅ [登出] 已清除认证信息')
  router.push('/login')
}

// 监听 storage 变化（多标签页同步）
const handleStorageChange = (e: StorageEvent) => {
  if (e.key === 'token' || e.key === 'userInfo') {
    updateUserInfo()
  }
}

onMounted(() => {
  updateUserInfo()
  window.addEventListener('storage', handleStorageChange)
  console.log('✅ [App] 应用已挂载')
})

onUnmounted(() => {
  window.removeEventListener('storage', handleStorageChange)
})
</script>

<style scoped>
#app {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.navbar {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 1rem 0;
  position: sticky;
  top: 0;
  z-index: 100;
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 2rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo {
  font-size: 1.5rem;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin: 0;
}

.nav-links {
  list-style: none;
  display: flex;
  gap: 2rem;
  margin: 0;
  padding: 0;
}

.nav-links a {
  text-decoration: none;
  color: #333;
  font-weight: 500;
  transition: color 0.3s;
}

.nav-links a:hover,
.nav-links a.router-link-active {
  color: #667eea;
}

.user-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.user-info {
  color: #667eea;
  font-weight: 500;
  font-size: 0.9rem;
}

.logout-btn,
.login-link {
  padding: 0.5rem 1rem;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 6px;
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  transition: opacity 0.3s, transform 0.2s;
}

.logout-btn:hover,
.login-link:hover {
  opacity: 0.9;
  transform: translateY(-1px);
}

.main-content {
  padding: 2rem;
}
</style>

